package com.luoshizhen.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoshizhen.lease.model.entity.*;
import com.luoshizhen.lease.web.admin.mapper.*;
import com.luoshizhen.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoshizhen.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.luoshizhen.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 根据查询条件分页获取协议信息
     *
     * @param page 分页对象，包含分页参数如当前页码、每页大小等
     * @param queryVo 查询条件封装对象，包含需要查询的协议相关条件
     * @return 返回分页后的协议信息列表，以AgreementVo对象形式封装
     */
    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.pageAgreementByQuery(page, queryVo);
    }

    /**
     * 根据ID获取协议详细信息
     * 此方法通过查询多个相关表来获取协议的详细信息，包括公寓、房间、支付方式和租期信息
     *
     * @param id 协议的唯一标识符
     * @return 返回一个包含协议详细信息的AgreementVo对象
     */
    @Override
    public AgreementVo getAgreementById(Long id) {
        //1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        //3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        //4.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());

        //5.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        //创建AgreementVo对象并复制租约信息
        AgreementVo adminAgreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement, adminAgreementVo);
        //设置公寓信息
        adminAgreementVo.setApartmentInfo(apartmentInfo);
        //设置房间信息
        adminAgreementVo.setRoomInfo(roomInfo);
        //设置支付方式
        adminAgreementVo.setPaymentType(paymentType);
        //设置租期
        adminAgreementVo.setLeaseTerm(leaseTerm);
        //返回包含详细信息的AgreementVo对象
        return adminAgreementVo;
    }
}




